package com.frank.frankutils;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;

/**
 * 网络工具类
 * 
 * @author Frank
 * @version 1.0 Create by 2015.12.25
 */
public class NetUtil {
	private static final String TAG = NetUtil.class.getSimpleName();
	private static final int LOW_SPEED_UPLOAD_BUF_SIZE = 1024;
	private static final int HIGH_SPEED_UPLOAD_BUF_SIZE = 10240;
	private static final int MAX_SPEED_UPLOAD_BUF_SIZE = 102400;
	private static final int LOW_SPEED_DOWNLOAD_BUF_SIZE = 2024;
	private static final int HIGH_SPEED_DOWNLOAD_BUF_SIZE = 30720;
	private static final int MAX_SPEED_DOWNLOAD_BUF_SIZE = 102400;

	/**
	 * 检查当前网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetworkAvailable(Activity activity) {
		Context context = activity.getApplicationContext();
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mobNetInfo = mConnectivityManager
					.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			NetworkInfo wifiNetInfo = mConnectivityManager
					.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			return isHaveNet(mobNetInfo, wifiNetInfo);
		}
		return false;
	}

	/**
	 * 是否有网
	 * 
	 * @param mobNetInfo
	 * @param wifiNetInfo
	 * @return
	 */
	public static boolean isHaveNet(NetworkInfo mobNetInfo,
			NetworkInfo wifiNetInfo) {
		if (mobNetInfo == null && wifiNetInfo == null) {
			return false;
		}
		if (mobNetInfo != null && mobNetInfo.isAvailable()
				&& mobNetInfo.isConnected()) {
			return true;
		}
		if (wifiNetInfo != null && wifiNetInfo.isAvailable()
				&& wifiNetInfo.isConnected()) {
			return true;
		}
		return false;
	}

	/**
	 * 是否有网络连接
	 * 
	 * @param paramContext
	 * @return
	 */
	public static boolean hasNetwork(Context paramContext) {
		if (paramContext != null) {
			ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
					.getSystemService("connectivity");
			NetworkInfo localNetworkInfo = localConnectivityManager
					.getActiveNetworkInfo();
			if (localNetworkInfo != null)
				return localNetworkInfo.isAvailable();
			return false;
		}
		return false;
	}

	/**
	 * 是否有数据连接
	 * 
	 * @param paramContext
	 * @return
	 */
	public static boolean hasDataConnection(Context paramContext) {
		ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
				.getSystemService("connectivity");
		NetworkInfo localNetworkInfo = localConnectivityManager
				.getNetworkInfo(1);
		if ((localNetworkInfo != null) && (localNetworkInfo.isAvailable())) {
			Logger.d(TAG, "有无线网络连接");
			return true;
		}
		localNetworkInfo = localConnectivityManager.getNetworkInfo(0);
		if ((localNetworkInfo != null)
				&& (localNetworkInfo.isConnectedOrConnecting())) {
			Logger.d(TAG, "移动连接");
			return true;
		}
		Logger.d(TAG, "没有数据连接");
		return false;
	}

	/**
	 * wifi是否链接
	 * 
	 * @param paramContext
	 * @return
	 */
	public static boolean isWifiConnection(Context paramContext) {
		ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
				.getSystemService("connectivity");
		NetworkInfo localNetworkInfo = localConnectivityManager
				.getNetworkInfo(1);
		if ((localNetworkInfo != null) && (localNetworkInfo.isConnected())) {
			Logger.d(TAG, "无线网络连接");
			return true;
		}
		return false;
	}

	/**
	 * 网络上传大小
	 * 
	 * @param paramContext
	 * @return
	 */
	public static int getUploadBufSize(Context paramContext) {
		ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
				.getSystemService("connectivity");
		NetworkInfo localNetworkInfo = localConnectivityManager
				.getActiveNetworkInfo();
		if ((localNetworkInfo != null) && (localNetworkInfo.getType() == 1))
			return 102400;
		if ((localNetworkInfo == null)
				&& (isConnectionFast(localNetworkInfo.getType(),
						localNetworkInfo.getSubtype())))
			return 10240;
		return 1024;
	}

	/**
	 * 网络下载大小
	 * 
	 * @param paramContext
	 * @return
	 */
	public static int getDownloadBufSize(Context paramContext) {
		ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
				.getSystemService("connectivity");
		NetworkInfo localNetworkInfo = localConnectivityManager
				.getActiveNetworkInfo();
		if ((localNetworkInfo != null) && (localNetworkInfo.getType() == 1))
			return 102400;
		if ((localNetworkInfo == null)
				&& (isConnectionFast(localNetworkInfo.getType(),
						localNetworkInfo.getSubtype())))
			return 30720;
		return 2024;
	}

	/**
	 * 
	 * @param paramInt1
	 * @param paramInt2
	 * @return
	 */
	private static boolean isConnectionFast(int paramInt1, int paramInt2) {
		if (paramInt1 == 1)
			return true;
		if (paramInt1 == 0) {
			switch (paramInt2) {
			case 7:
				return false;
			case 4:
				return false;
			case 2:
				return false;
			case 5:
				return true;
			case 6:
				return true;
			case 1:
				return false;
			case 8:
				return true;
			case 10:
				return true;
			case 9:
				return true;
			case 3:
				return true;
			}
			if ((Build.VERSION.SDK_INT >= 11)
					&& ((paramInt2 == 14) || (paramInt2 == 13)))
				return true;
			if ((Build.VERSION.SDK_INT >= 9) && (paramInt2 == 12))
				return true;
			if ((Build.VERSION.SDK_INT >= 8) && (paramInt2 == 11))
				return false;
		}
		return false;
	}

	/**
	 * 获得网络类型
	 * @param paramContext
	 * @return
	 */
	public static String getNetworkType(Context paramContext) {
		ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
				.getSystemService("connectivity");
		NetworkInfo localNetworkInfo = localConnectivityManager
				.getActiveNetworkInfo();
		if ((localNetworkInfo != null) && (localNetworkInfo.isAvailable())) {
			int i = localNetworkInfo.getType();
			if (i == 1)
				return "WIFI";
			TelephonyManager localTelephonyManager = (TelephonyManager) paramContext
					.getSystemService("phone");
			switch (localTelephonyManager.getNetworkType()) {
			case 1:
			case 2:
			case 4:
			case 7:
			case 11:
				return "2G";
			case 3:
			case 5:
			case 6:
			case 8:
			case 9:
			case 10:
			case 12:
			case 14:
			case 15:
				return "3G";
			case 13:
				return "4G";
			}
			return "unkonw network";
		}
		return "no network";
	}
}
